package org.seedltd.upms.controller.detection;

import java.io.IOException;

import org.seedltd.base.exception.BusinessException;
import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.StringUtils;
import org.seedltd.core.validation.Save;
import org.seedltd.core.validation.Update;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.detection.form.DetectionTubeForm;
import org.seedltd.upms.model.detection.form.DetectionTubeSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionTubeVO;
import org.seedltd.upms.service.detection.DetectionTubeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import lombok.AllArgsConstructor;

/**
 *
 * 试剂管维护控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/detectionTube")
public class DetectionTubeController extends BaseController<DetectionTubeService> {

    /**
     * 新增
     * 
     * @param form
     *            试剂管维护表单数据
     * @return 试剂管维护主键
     */
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('detection:tube:add')")
    public Long add(@RequestBody @Validated({Save.class}) DetectionTubeForm form) {
        return baseService.add(form);
    }

    /**
     * 获取详情
     * 
     * @param id
     *            试剂管维护主键
     * @return 试剂管维护
     */
    @GetMapping(value = "/get")
    public DetectionTubeVO get(Long id) {
        Assert.notNull(id, "参数不能为空");
        return baseService.findById(id);
    }

    /**
     * 修改
     * 
     * @param form
     *            试剂管维护表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @SysLog("修改试剂管数据")
    @PostMapping(value = "/update")
    @PreAuthorize("hasAuthority('detection:tube:update')")
    public boolean update(@Validated({Update.class, Save.class}) DetectionTubeForm form, MultipartFile[] file)
        throws IOException {
        baseService.update(form, file);
        return true;
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 试剂管维护列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<DetectionTubeVO> page(@RequestBody DetectionTubeSearchForm searchForm) {
        return baseService.page(searchForm);
    }

    /**
     * 批量上传检测报告
     * 
     * @param file
     *            压缩文件
     * @return 消息内容
     */
    @PostMapping("/uploadReportByZip")
    public String uploadReportBatch(MultipartFile file) throws IOException {
        Assert.notNull(file, "请上传文件");
        if (!StringUtils.endsWith(file.getOriginalFilename(), ".zip")) {
            throw new BusinessException("请上传zip格式的文件");
        }
        return baseService.uploadReportBatch(file);
    }

}